/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyphenate.chatuidemo;

import android.bluetooth.le.ScanRecord;
import android.content.Context;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.support.multidex.MultiDex;

import com.hyphenate.EMCallBack;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.Reserved;
import com.joplus.housekeeper.bean.User;
import com.joplus.housekeeper.bean.XmppUser;
import com.joplus.housekeeper.core.Config;
import com.joplus.housekeeper.utils.L;
import com.joplus.housekeeper.utils.SPUtils;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;


import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.data.JPushLocalNotification;
import io.realm.Realm;

public class DemoApplication extends android.support.multidex.MultiDexApplication {


    String TAG = "DemoApplication";
    public static Context applicationContext;
    private static DemoApplication instance;

    public static int SHARE_CODE = -1;
    // login user name
    public final String PREF_USERNAME = "username";

    /**
     * 当前用户nickname,为了苹果推送不是userid而是昵称
     */
    public static String currentUserNick = "";
    public static String successShow = " ";

    private Reserved reserved;
    private String customerAvatar;
    // public static Boolean isInterestEditEnable = false;


    private boolean bindService;
    private ServiceConnection conn;
    private ScanRecord account;
    private String messageId;
    private String rid;

    public void setReserved(Reserved reserved) {
        this.reserved = reserved;
    }

    public String getCustomAvatar() {
        return customerAvatar;
    }

    public void setCustomAvatar(String image) {
        customerAvatar = image;
    }

    /**
     * 获取订单详情
     *
     * @return
     */
    public Reserved getReserved() {
        return reserved;
    }

    private XmppUser user;
    private User _user;

    public void setXmppUser(XmppUser user) {
        this.user = user;

    }

    public void setUser(User uesr) {
    }

    public void setAccountId(String ss) {

    }

    public boolean isLogin() {
        return true;
    }

    public String getAccountId() {
        return "";
    }

    public User getUser() {
        return _user;
    }

    public XmppUser getXmppUser() {
        return user;
    }


    public String customerJid;

    public void setJid(String id) {
        customerJid = id;
    }

    public String getJid() {
        return customerJid;
    }


    public void setVipType(String vipType) {

    }


    @Override
    public void onCreate() {
        MultiDex.install(this);
        super.onCreate();
        applicationContext = this;
        instance = this;
        //init demo helper
        DemoHelper.getInstance().init(DemoApplication.getInstance());
        JPushInterface.init(this);
        JPushInterface.setDebugMode(true);
        JPushInterface.addLocalNotification(getApplicationContext(), new JPushLocalNotification());
        rid = JPushInterface.getRegistrationID(getApplicationContext());

        initImageLoader();

        //    initTalkingData();

        Realm.init(this);
    }

    public String getRid() {
        return rid;
    }

    public static DemoApplication getInstance() {
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }


    public void exitApp() {




        if (DemoHelper.getInstance().isLoggedIn()) {
            DemoHelper.getInstance().logout(false, new EMCallBack() {
                @Override
                public void onSuccess() {
                    L.e("环信退出登录 onSuccess");
                }

                @Override
                public void onError(int i, String s) {

                    L.e("环信退出登录 onError" + s);
                }

                @Override
                public void onProgress(int i, String s) {

                }
            });

        }

        SPUtils.put(applicationContext, Config.PHONE, "");
        SPUtils.put(applicationContext, Config.ACCOUNTID, "");

    }



    /**
     * 初始化图片加载
     */
    private void initImageLoader() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
//                .showImageOnLoading(R.drawable.loading) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.jo_placebudengwei) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.jo_placebudengwei) // 设置图片加载或解码过程中发生错误显示的图片
                .resetViewBeforeLoading(false)  // default 设置图片在加载前是否重置、复位
                .delayBeforeLoading(0)  // 下载前的延迟时间
                .cacheInMemory(false) // default  设置下载的图片是否缓存在内存中
                .cacheOnDisk(true) // default  设置下载的图片是否缓存在SD卡中
                .considerExifParams(false) // default
                .imageScaleType(ImageScaleType.EXACTLY) //  设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565) // default 设置图片的解码类型
//                .displayer(new SimpleBitmapDisplayer()) // default  还可以设置圆角图片new RoundedBitmapDisplayer(20)

                .handler(new Handler()) // default
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)

//                .diskCacheExtraOptions(400,400,null)
//                .discCacheExtraOptions(480,800, BitmapProcessor.CompressFormat.JPEG,75,null)
//                .diskCacheExtraOptions(480,800,new )
                .threadPoolSize(3) // default  线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级
                .tasksProcessingOrder(QueueProcessingType.FIFO) // default
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现
                .memoryCacheSize(2 * 1024 * 1024)  // 内存缓存的最大值
                .memoryCacheSizePercentage(13) // default
                //.memoryCache(new WeakMemoryCache())
                .diskCacheSize(20 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
                .imageDownloader(new BaseImageDownloader(this)) // default
                .imageDecoder(new BaseImageDecoder(false)) // default
                .defaultDisplayImageOptions(options) // default
                .diskCacheExtraOptions(480, 320, null)
                .discCacheFileNameGenerator(new Md5FileNameGenerator() {
                    @Override
                    public String generate(String imageUri) {
                        String imageName = imageUri;

                        Uri u = Uri.parse(imageUri);

                        // imageName =u.getHost();

                        try {
                            imageName = imageUri.split("[?]e=")[0];

                        } catch (Exception e) {
                            // e.printStackTrace();
                        }
                        return super.generate(imageName);
                    }
                })//
                .writeDebugLogs() // 打印debug log
                .build(); //开始构建
        ImageLoader.getInstance().init(config);


    }

    public String getVipType() {
        return " ";
    }

    public String getAccount() {
        return " ";
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }
}
